package com.example.baiguangsystem.service.impl;

import com.example.baiguangsystem.entity.Author;
import com.example.baiguangsystem.entity.Section;
import com.example.baiguangsystem.mapper.SectionMapper;
import com.example.baiguangsystem.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 章节表的service层实现
 *
 * @Author: 27641
 * @ProjectName: BaiGuangSystem
 * @Date: 2022/02/05/22:26
 * @noinspection SpellCheckingInspection
 */
@Service
public class SectionServiceImpl implements SectionService {

    /**
     * 注入依赖
     */
    @Autowired
    private SectionMapper sectionMapper;

    /**
     * 根据书本Id查找该书本的所有信息
     *
     * @param bookId 需要查找的书本Id
     * @return 查找结果
     */
    @Override
    public List<Section> findSectioAllByBookId(Integer bookId) {
        return this.sectionMapper.findSectionByBookId(bookId);
    }

    /**
     * 添加新章节给指定书本Id的书本表
     *
     * @param section 新增章节
     * @param integer 需要新增书本的Id
     */
    @Override
    public void newAddSectionByBookId(Section section, Integer integer) {
        this.sectionMapper.newAddSectionByBookId(section, integer);
    }

    /**
     * 根据章节Id删除章节(使用事务控制)
     *
     * @param sectionId 需要删除的章节Id
     * @return 删除的章节信息
     */
    @Transactional
    @Override
    public Section deleteSectionByBookId(Integer sectionId) {
        Section section = this.findSectionById(sectionId);
        this.sectionMapper.deleteSectionByBookId(sectionId);
        return section;
    }

    /**
     * 根据章节Id查找章节信息
     *
     * @param sectionId 需要查找的章节Id
     * @return 查找结果
     */
    @Override
    public Section findSectionById(Integer sectionId) {
        return this.sectionMapper.findSectionMessagesBySectionId(sectionId);
    }

}
